package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.AtWho;
import cn.wolfcode.trip.base.domain.Commentait;
import cn.wolfcode.trip.base.domain.Questionnaire;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.AtWhoMapper;
import cn.wolfcode.trip.base.mapper.CommentaitMapper;
import cn.wolfcode.trip.base.mapper.QuestionnaireMapper;
import cn.wolfcode.trip.base.query.QuestionnaireQueryObject;
import cn.wolfcode.trip.base.service.IAtWhoService;
import cn.wolfcode.trip.base.service.IQuestionnaireService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionnaireServiceImpl implements IQuestionnaireService {
    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public PageInfo selectProblem(QuestionnaireQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Questionnaire> list = questionnaireMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //查询所有问题
    @Override
    public List selectProblems() {
        return questionnaireMapper.selectAll();
    }

    //查询所有答案
    @Override
    public List selectAnswer() {
        return answerMapper.selectAll();
    }

    //查询所有问题的id
    @Override
    public List getQuestionnaireId() {
        return questionnaireMapper.getQuestionnaireId();
    }

    //保存用户填写调查问卷信息
    @Override
    public void saveOrUpdate(Long userId,String value) {
        //value 索引0为问题id,1为答案id
        //将字符串切割
        String[] ids=value.split("-");
        //用来装问题的id
        Long problemId=Long.parseLong(ids[0]);
        //用来装答案的id
        Long answerId=Long.parseLong(ids[1]);
        //将问题的id和用户的id作为查询条件到后台做数据查询,如果有值证明这个用户的这条问题有填过答案,则作修改操作
        Questionnaire questionnaire = questionnaireMapper.selectResultByIds(userId,problemId);
        if (questionnaire==null){
            questionnaireMapper.insertQuestionnaireMessage(userId,problemId,answerId);
        }else {
            //修改操作,只需要修改答案即可,条件为同时满足问题和用户id
            questionnaireMapper.updateQuestionnaireMessage(userId,problemId,answerId);
        }
    }

    //保存用户的意见和建议
    @Override
    public void saveEvaluates(Long userId,String value) {
        int count = questionnaireMapper.selectEvaluatesByUserId(userId);
        if (count==0){
            questionnaireMapper.insertEvaluates(userId,value);
        }else if(count==1){
            questionnaireMapper.updateEvaluates(userId,value);
        }else {
            throw new RuntimeException("出现错误,请联系管理员!");
        }
    }

    //后台查询用户填写的调查信息
    @Override
    public PageInfo query(QuestionnaireQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(questionnaireMapper.selectQuestionnaire(qo));
    }

    //后台查询用户填写的意见和建议
    @Override
    public PageInfo queryEvaluate(QuestionnaireQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(questionnaireMapper.selectEvaluate(qo));
    }

    //查询问题和答案,将其合成字符串,进行回显
    @Override
    public List<String> selectQuestionnaire(Long userId) {
        //新建一个集合,用来装合成的字符串
        List<String> strings=new ArrayList<>();
        //查出该用户所有调查内容对象,进行遍历,合成字符串
        List<Questionnaire> list = questionnaireMapper.selectQuestionnaireByUserId(userId);
        if (list==null){
            return null;
        }
        for (Questionnaire questionnaire : list) {
            Long problemId = questionnaire.getId();
            Long answerId = questionnaire.getAnswer().getId();
            String str=problemId+"-"+answerId;
            strings.add(str);
        }
        return strings;
    }
}
